﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace GUI
{
    public partial class FolderList : UserControl
    {
        private string[] paths = {""};
        private ControlCollection controls;

        public FolderList NextList { get; set; }
        public string[] SearchPath
        {
            get
            {
                if (NextList == null)
                    return paths;
                else
                    return NextList.SearchPath;
            }
        }

        public FolderList(string path, ControlCollection controls)
        {
            InitializeComponent();

            paths[0] = path;
            FillList(path);

            this.controls = controls;
        }


        public FolderList(string[] paths, ControlCollection controls)
        {
            InitializeComponent();

            this.paths = paths;
            foreach (string path in paths)
                FillList(path);

            this.controls = controls;
        }

        private void FillList(string path)
        {
            DirectoryInfo di = new DirectoryInfo(path);
            DirectoryInfo[] dirs = di.GetDirectories();


            foreach (DirectoryInfo dir in dirs)
            {
                /*
                 * if directorycollection does have the name of dir allready
                 * then add to the directorycollection
                 * eles add to the listbox collection
                 */
                bool found = false;
                foreach (DirectoryCollection directoryCollection in lbFolders.Items)
                {
                    if (directoryCollection.Name == dir.Name)
                    {
                        directoryCollection.Directories.Add(dir);
                        found = true;
                        break;
                    }
                }

                if (!found)
                    lbFolders.Items.Add(new DirectoryCollection(dir));
            }
        }

        private void RemoveChild()
        {
            if (NextList != null)
            {
                NextList.RemoveChild();
                controls.Remove(NextList);
            }
        }

        private void lbFolders_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (NextList != null)
            {
                NextList.RemoveChild();
                controls.Remove(NextList);
            }

            /*
             * if got something selected
             * we need to get the selected paths and send to the next FolderList for processing.
             */
            if (lbFolders.SelectedItems.Count > 0)
            {
                List<string> paths = new List<string>();

                // Look at each selected item and add to a list of paths to show next
                foreach (object selectedItem in lbFolders.SelectedItems)
                    if (selectedItem != null && selectedItem is DirectoryCollection)
                        foreach (DirectoryInfo directoryInfo in (selectedItem as DirectoryCollection).Directories)
                            paths.Add(directoryInfo.FullName);

                // make the next FolderList
                NextList = new FolderList(paths.ToArray(), controls);
                controls.Add(NextList);
            }
        }

        private class DirectoryCollection
        {
            public IList<DirectoryInfo> Directories { get; set; }
            public string FullName { get { return Directories[0].FullName; } }
            public string Name { get { return Directories[0].Name; } }

            public DirectoryCollection(DirectoryInfo directoryInfo)
            {
                Directories = new List<DirectoryInfo>();
                Directories.Add(directoryInfo);
            }

            public DirectoryCollection(string path) : this(new DirectoryInfo(path)) { }
        }
    }
}
